#!/bin/bash

function s1_generate_a_series_of_files() {
for index in $(seq 1 100); do
echo -n "$index" > "/tmp/new_file$index"
done
}

function s2_list_4_files_in_long_format_about_account_and_group() {
ls -l $(find /etc -name "passwd" -or -name "shadow" -or -name "group" -or -name "gshadow" | sort )
}

function s3_find_items_related_to_given_username() {
if [ -z "$1" ]; then return 1; fi
grep "$1" /etc/passwd /etc/shadow | sort
}

function s4_find_items_related_to_given_group_name() {
if [ -z "$1" ]; then return 1; fi
grep "$1" /etc/group /etc/gshadow | sort
}

function s5_create_groups_for_given_group_names() {
groupadd -f rnd
groupadd -f itoperation
}

function s6_create_users_and_set_their_group_as_required() {
useradd -M -N -s /bin/bash adam || true
useradd -M -N -s /bin/bash bob || true
useradd -M -N -s /bin/bash calvin || true
useradd -M -N -s /bin/bash david || true
useradd -M -N -s /bin/bash edward || true
usermod -aG rnd adam bob edward || true
usermod -aG itoperation calvin david || true
}

function s7_change_users_shell() {
chsh -s /usr/bin/sh adam || true
chsh -s /usr/bin/sh edward || true
}

function s8_set_password_change_policy() {
for user in adam bob calvin david edward; do
chage -m 10 -M 60 -W 10 "$user" || true
done
}

function s9_set_password_for_five_users() {
if [ $# -ne 5 ]; then return 1; fi
usernames=("adam" "bob" "calvin" "david" "edward")
for i in $(seq 1 5); do
echo "${usernames[$((i-1))]}:${!i}"
done | chpasswd
}

function s10_lock_bobs_password() {
passwd -l bob || true
}

function s11_include_adam_into_wheel_group() {
usermod -aG wheel adam || true
}

function s12_change_files_permission() {
for i in $(seq 3 3 99); do
chmod 640 "/tmp/new_file$i"
done
}

function main() {
s1_generate_a_series_of_files
s2_list_4_files_in_long_format_about_account_and_group
s3_find_items_related_to_given_username "adam"
s4_find_items_related_to_given_group_name "rnd"
s5_create_groups_for_given_group_names
s6_create_users_and_set_their_group_as_required
s7_change_users_shell
s8_set_password_change_policy
s10_lock_bobs_password
s11_include_adam_into_wheel_group
s12_change_files_permission
}
main
